<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
      <el-form-item label="一级分类" >
        <el-select placeholder="请选择" v-model="cForm.category1Id" @change="handler1" :disabled="show">
          <el-option :label="c1.name" :value="c1.id" v-for="c1 in List1" :key="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" >
        <el-select placeholder="请选择" v-model="cForm.category2Id" @change="handler2" :disabled="show">
          <el-option :label="c2.name" :value="c2.id" v-for="c2 in List2" :key="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类" >
        <el-select placeholder="请选择" v-model="cForm.category3Id" @change="handler3" :disabled="show">
          <el-option :label="c3.name" :value="c3.id" v-for="c3 in List3" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "categourySelect",
  data() {
    return {
      //一级数据
      List1:[],
      //一级二级三级数据id
      cForm:{
        category1Id:'',
        category2Id:'',
        category3Id:'',
      },
      //二级数据
      List2:[],
      //三级数据
      List3:[]
    }
  },
  mounted() {
    this.getCategory1List()
  },
  props:['show'],
  methods:{
    //一级分类
    async getCategory1List() {
      let reslut = await this.$API.attr.reqCategory1List()
      if(reslut.code == 200) {
        this.List1 = reslut.data
      }
    },
    //获取二级分类
    async handler1() {
      this.List2 = []
      this.List3 = []
      this.cForm.category2Id = ''
      this.cForm.category3Id = ''
      let reslut = await this.$API.attr.reqCategory2List(this.cForm.category1Id)
      this.$emit('getcategoryId',{categoryId:this.cForm.category1Id,level:1})
      
      if(reslut.code == 200) {
        
        this.List2 = reslut.data
      }
    },
    //获取三级分类
    async handler2() {
      this.List3 = []
      this.cForm.category3Id = ''
      let reslut = await this.$API.attr.reqCategory3List(this.cForm.category2Id)
      this.$emit('getcategoryId',{categoryId:this.cForm.category2Id,level:2})
      if(reslut.code == 200) {
        this.List3 = reslut.data
      }
    },
    handler3() {
      
      this.$emit('getcategoryId',{categoryId:this.cForm.category3Id,level:3})
    }
  }
};
</script>

<style></style>
